﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using TecExtensions;

namespace TecBaseObjects.Objects {
    public class SheetObj {

        #region Members

        private Extensions.SheetType sheetType = Extensions.SheetType.Unbekannt;
        private Worksheet mySheet;

        #endregion

        #region Constructor

        public SheetObj(Worksheet sh) {
            mySheet = sh;
        }
                
        #endregion

        #region Properties

        public Extensions.SheetType SheetType {
            get {
                Extensions.SheetType st;
                string sheetType = mySheet.Name.Substring(1);
                foreach (CustomProperty p in mySheet.CustomProperties) {
                    if (p.Name == "SheetType") {
                        object ret = p.Value;
                        st = Enum.Parse(typeof(Extensions.SheetType), p.Value.ToString());
                        return st;
                    }
                }

                if (sheetType.Contains("k")) {
                    return Extensions.SheetType.Kalkulation;
                }
                if (sheetType.Contains("r")) {
                    return Extensions.SheetType.Abrechnung; //***
                }
                if (sheetType.Contains("aa") | sheetType.Contains("ab") | sheetType.Contains("ac") | sheetType.Contains("ad") | sheetType.Contains("ae") | sheetType.Contains("af") | sheetType.Contains("ag")) {
                    return Extensions.SheetType.Auftragsbestaetigung; //***
                }
                if (sheetType.Contains("b")) {
                    return Extensions.SheetType.Projektbericht; //***
                }
                if (sheetType.Contains("l")) {
                    return Extensions.SheetType.Lieferschein;
                }
                if (sheetType.Contains("f")) {
                    return Extensions.SheetType.Rechnung; //***
                }
                if (sheetType.Contains("p")) {
                    return Extensions.SheetType.Abnahmeprotokoll; //***
                }
                if (sheetType.Contains("ka") | sheetType.Contains("kb") | sheetType.Contains("kc") | sheetType.Contains("kd") | sheetType.Contains("ke") | sheetType.Contains("kf") | sheetType.Contains("kg")) {
                    return Extensions.SheetType.Kalkulation;
                }
                if (sheetType.Contains("a")) {
                    return Extensions.SheetType.Angebot; //***
                }
                return this.sheetType;
            }
        }

        public string OrgName {
            get {
                string ret = "";
                Range r1 = mySheet.Cells[2,3];
                if (r1.Value != null) {
                    ret = Convert.ToString(r1.Value);
                }
                return ret;
            }
        }

        public int ProjektNummer {
            get {
                int proNr = 0;
                string proj = mySheet.Name.Substring(1);
                string n1 = Extensions.Left(proj, 2);
                if (Extensions.isNumeric(n1, System.Globalization.NumberStyles.Any)) {
                    proNr = Convert.ToInt32(n1);
                    return proNr;
                }
                n1 = Extensions.Left(proj, 1);
                if (Extensions.isNumeric(n1, System.Globalization.NumberStyles.Any)) {
                    proNr = Convert.ToInt32(n1);
                    return proNr;
                }
                return proNr;
            }
        }

        public double SummeBrutto {
            get {
                double ret = 0.0;
                if (SheetType == Extensions.SheetType.Rechnung) {
                    if (ExistsName("SummeBrutto")) {
                        Range t1 = mySheet.Range["SummeBrutto"];
                        double rb = 0.0;
                        Double.TryParse(t1.Value.ToString(), out rb);
                        return rb;
                    }
                }
                return ret;
            }
        }

        #endregion

        #region Functions

        public bool ExistsName(string name) {
            foreach (Name n1 in mySheet.Names) {
                if (n1.Name.Contains(mySheet.Name) & n1.Name.Contains(name)) {
                    if (!n1.Value.Contains("#REF!")) {
                        return true;
                    }
                }
            }
            return false;
        }

        #endregion

    }
}
